<?php

class Sermon extends Admin_Controller {

    /**
     * Int Blog categoryID
     * @var type 
     */
    protected $_categoryID = 3;

    /**
     * Array that contains the validation rules
     * @access protected
     * @var array
     */
    protected $validation_rules = array(
        'title' => array(
            'field' => 'title',
            'label' => 'Tiêu đề',
            'rules' => 'trim|htmlspecialchars|required|max_length[200]'
        ),
        array(
            'field' => 'keywords',
            'label' => 'Từ khóa',
            'rules' => 'trim'
        ),
        array(
            'field' => 'intro',
            'label' => 'Mô tả bài viết',
            'rules' => 'trim|max_length[200]'
        ),
        array(
            'field' => 'content',
            'label' => 'Mô tả bài viết',
            'rules' => 'trim'
        )
    );

    public function __construct() {
        parent::__construct();

        //Load blog model
        $this->load->model('backend/blog_m');
    }

    /**
     * Default function
     */
    public function index() {
        //Load categories model
        $this->load->model('backend/categories_m');
        //Get array category of blog
        $arrCategories = $this->categories_m->get_all_by_parent_id($this->_categoryID);

        //Get array CategoryID
        $arrCategoryID = array($this->_categoryID);
        $arrCategoryName = array($this->_categoryID => 'None');
        if ($arrCategories) {
            foreach ($arrCategories as $category) {
                $arrCategoryID[] = $category['id'];
                $arrCategoryName[$category['id']] = $category['title'];
            }
        }

        //Get array blogs
        $arrData = $this->blog_m->get_object_in('category_id', $arrCategoryID);
        
        //Assign to view
        $this->assign('arrData', $arrData);
        $this->assign('arrCategoryName', $arrCategoryName);

        //Set layout title
        $this->layout_title = 'Quản lý sứ điệp';
    }

    /**
     * Create new object
     */
    public function create() {
        //Load validation lib
        $this->load->library('form_validation');
        //Set rules
        $this->form_validation->set_rules($this->validation_rules);

        //Check is post
        if ($this->input->post()) {
            if ($this->form_validation->run()) {
                //Load keyword library
                $this->load->library('keywords');

                //Get current user
                $globals = $this->load->get_var('_globals');

                $arrObject = array(
                    'category_id' => $this->input->post('category'),
                    'title' => $this->input->post('title'),
                    'slug' => $this->removeUnicode($this->input->post('title')),
                    'thumb' => $this->input->post('thumbnail_url'),
                    'keywords' => $this->keywords->process($this->input->post('keywords')),
                    'intro' => $this->input->post('intro'),
                    'body' => $this->input->post('content'),
                    'status' => $this->input->post('status'),
                    'created_on' => time(),
                    'updated_on' => time(),
                    'comments_enabled' => $this->input->post('comment'),
                    'author_id' => $globals['user']['id'],
                    'type' => $this->input->post('object_type'),
                );

                $arrObjectExtend = array(
                    'video_url' => $this->input->post('video_url'),
                    'audio_url' => $this->input->post('audio_url'),
                    'file_url' => $this->input->post('file_url')
                );
                
                $id = $this->blog_m->insert_object($arrObject, $arrObjectExtend);

                if ($id) {
                    $this->session->set_flashdata('success', sprintf('Đã thêm bài viết "%s".', $this->input->post('title')));
                } else {
                    $this->session->set_flashdata('error', 'Có lỗi xảy ra.');
                }

                redirect('backend/sermon/create');
            }
        }

        //Load categories model
        $this->load->model('backend/categories_m');
        //Get array category of blog
        $arrCategory = $this->categories_m->get_all_by_parent_id($this->_categoryID);

        //Assign to view
        $this->assign('arrCategory', $arrCategory);
        $this->assign('rootCategory', $this->_categoryID);

        $this->layout_title = 'Viết bài mới';
    }

    /**
     * Edit object
     */
    public function update() {
        //Load validation lib
        $this->load->library('form_validation');
        //Load keyword library
        $this->load->library('keywords');
        //Set rules
        $this->form_validation->set_rules($this->validation_rules);

        //Get object_id
        $objID = $this->uri->segment(5);

        //Check object_id
        if (empty($objID)) {
            redirect('/backend/sermon');
        }

        //Check is post
        if ($this->input->post()) {
            if ($this->form_validation->run()) {
                //Get current user
                $globals = $this->load->get_var('_globals');
                
                $arrObject = array(
                    'category_id' => $this->input->post('category'),
                    'title' => $this->input->post('title'),
                    'slug' => $this->removeUnicode($this->input->post('title')),
                    'thumb' => $this->input->post('thumbnail_url'),
                    'keywords' => $this->keywords->process($this->input->post('keywords')),
                    'intro' => $this->input->post('intro'),
                    'body' => $this->input->post('content'),
                    'status' => $this->input->post('status'),
                    'created_on' => time(),
                    'updated_on' => time(),
                    'comments_enabled' => $this->input->post('comment'),
                    'author_id' => $globals['user']['id'],
                    'type' => $this->input->post('object_type'),
                );

                $arrObjectExtend = array(
                    'video_url' => $this->input->post('video_url')
                );

                //Update object data
                $result = $this->blog_m->update_object($objID, $arrObject, $arrObjectExtend);

                if ($result) {
                    $this->session->set_flashdata('success', sprintf('Đã chỉnh sửa bài viết "%s" thành công.', $this->input->post('title')));
                } else {
                    $this->session->set_flashdata('error', 'Có lỗi xảy ra.');
                }

                redirect('backend/sermon/update/id/' . $objID);
            }
        } else {
            //Get object detail
            $arrObjDetail = $this->blog_m->get_object_detail($objID);

            //Check array Object
            if (empty($arrObjDetail)) {
                redirect('/backend/sermon');
            }

            //Get keyword
            if (!empty($arrObjDetail['keywords'])) {
                $arrObjDetail['keywords'] = $this->keywords->get_string($arrObjDetail['keywords']);
            }
            
            //Load categories model
            $this->load->model('backend/categories_m');
            //Get array category of blog
            $arrCategory = $this->categories_m->get_all_by_parent_id($this->_categoryID);

            //Assign to view
            $this->assign('arrCategory', $arrCategory);
            $this->assign('rootCategory', $this->_categoryID);

            //Assign to view
            $this->assign('arrObjDetail', $arrObjDetail);

            //Set layout title
            $this->layout_title = 'Chỉnh sửa bài viết';
        }
    }

    /**
     * Delete object by ojectID
     */
    public function delete() {
        //Get object_id
        $objID = $this->uri->segment(5);

        //Check object_id
        if (empty($objID)) {
            redirect('/backend/sermon');
        }

        //Delete object
        $this->blog_m->delete_object($objID);

        //Return 
        redirect('/backend/sermon');
    }

}